package com.aos.service.role.impl;

import com.aos.dao.RoleDao;
import com.aos.entity.Role;
import com.aos.exception.BusinessException;
import com.aos.exception.ResultStatus;
import com.aos.service.role.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("RoleService")
public class RoleServiceImpl implements RoleService {

    private static Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    RoleDao roleDao;

    @Override
    public int insertRole(long rid, String rname) {

        System.out.println("========开始校验角色信息是否存在========");

        //首先校验Role表是否已有数据，有则提示
        Role existRole = roleDao.findByRid(rid);
        if(existRole != null)
        {
            throw new BusinessException("该角色ID已存在，无法新增！", ResultStatus.fail.toString());
        }

        System.out.println("========开始校执行新增操作，rid:[" + rid + "]");

        //不存在此角色信息，则新增
        int count = roleDao.insertRoleByRid(rid,rname);
        return count;
    }

    @Override
    public int updateRole(long rid, String rname) {
        //首先校验Role是否有此角色信息，没有则提示
        Role existRole = roleDao.findByRid(rid);
        if(existRole == null)
        {
            throw new BusinessException("该角色信息不存在，无法修改！", ResultStatus.fail.toString());
        }
        System.out.println("========开始执行角色更新操作，rid:["+ existRole.getRid() + "]");
        //存在则更新信息
        int count = roleDao.updateRoleByRid(rid,rname);
        return count;
    }

    @Override
    public int deleteRole(long rid) {
        //首先校验Role是否有此角色信息，没有则提示
        Role existRole = roleDao.findByRid(rid);
        if(existRole == null)
        {
            throw new BusinessException("该角色信息不存在，无法删除！", ResultStatus.fail.toString());
        }

        System.out.println("========开始执行角色删除操作，rid:["+ existRole.getRid() + "]");
        //存在则删除
        int count = roleDao.deleteRoleByRid(rid);
        return count;
    }
}
